<script setup lang="ts">
import { onMounted, ref } from 'vue';

const inpRef = ref<HTMLInputElement | null>(null)
onMounted(() => {
  // 1. 非空断言
  inpRef.value!.focus()

  // 2. 判断
  if (inpRef.value) {
    inpRef.value.focus()
  }

  // 3. 可选链
  inpRef.value?.focus()
})

const aRef = ref<HTMLAnchorElement | null>(null)
onMounted(() => {
  aRef.value!.href = '585858'
})

</script>

<template>
  <div>
    <input type="text" ref="inpRef">
    <a href="#" ref="aRef">点我</a>
  </div>
</template>

<style scoped></style>
